package com.example.coffeeseller_2.model.model;

import android.text.TextUtils;

import com.example.coffeeseller_2.model.MachineConfig.QRMsger;
import com.example.coffeeseller_2.model.beans.DealRecorder;
import com.example.coffeeseller_2.model.beans.MachineConfig;
import com.example.coffeeseller_2.model.contentprovider.Constance;
import com.example.coffeeseller_2.model.contentprovider.ConstanceMethod;
import com.example.coffeeseller_2.model.factory.PayState;
import com.example.coffeeseller_2.model.httputils.JsonUtil;
import com.example.coffeeseller_2.model.httputils.OkHttpUtil;
import com.example.coffeeseller_2.model.listenner.MsgTransListener;
import com.example.coffeeseller_2.utils.MyLog;

import java.io.IOException;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by WH on 2018/5/1.
 */

public class TradeMsgRequest {
    private static String TAG = "TradeMsgRequest";
    public static int DEFAULT = -1;
    public int mCurRequest = DEFAULT;
    public static int REQUEST_QR_CODE = 0;
    public static int REQUEST_CHECK_PAY = 1;
    private TimerTask mCheckPayTask = null;
    private Timer mTimer = null;

    /*
* 请求二维码
* */
    public void requestQRCode(MsgTransListener msgTransListener) {

        MyLog.d(TAG, "requestQRCode");
        mCurRequest = REQUEST_QR_CODE;
        QRMsger qrMsger = new QRMsger();
        qrMsger.reqQR(msgTransListener, null);
    }

    /*
    * 关闭订单
    * */
    public void closeOrder(final String order) {
        Runnable mRun = new Runnable() {

            @Override
            public void run() {
                Map<String, Object> params = ConstanceMethod.getParams();
                params.put("machineCode", MachineConfig.getMachineCode());
                params.put("tradeCode", order);

                MyLog.d(TAG, "cancleOrder RQ_URL = " + Constance.TRADE_CLOSE);
                MyLog.d(TAG, "cancleOrder params = " + JsonUtil.mapToJson(params));

                String RESPONSE_TEXT = null;
                try {
                    RESPONSE_TEXT = OkHttpUtil.post(Constance.TRADE_CLOSE, JsonUtil.mapToJson(params));
                } catch (IOException e) {
                    e.printStackTrace();
                }

                MyLog.W(TAG, "cancleOrder data" + RESPONSE_TEXT);
            }
        };
        Thread t = new Thread(mRun);
        t.start();
    }

    /*
    * 开始检查订单是否支付
    * */
    public void beginTaskCheckPay(final MsgTransListener msgTransListener) {

        /*
        * 订单号不为空时才能开启轮询
        * */
        if (TextUtils.isEmpty(DealRecorder.getInstance().getOrder())) {
            return;
        }
        mCurRequest = REQUEST_CHECK_PAY;

        if (mTimer == null) {

            mTimer = new Timer(true);
        }
        if (mCheckPayTask == null) {

            mCheckPayTask = new TimerTask() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    DealRecorder.getInstance().getPayState();
                    if ((DealRecorder.getInstance().getPayState() != PayState.PAYED) && DealRecorder.getInstance().isVilid()) {
                        QRMsger qrMsger = new QRMsger();
                        qrMsger.checkPay(msgTransListener, DealRecorder.getInstance().getOrder());
                    } else {
                        stopTaskCheckPay();
                    }
                }
            };

            mTimer.schedule(mCheckPayTask, 10000, 3000);//交易之后20秒进行查询交易状态,每隔5秒查询一次
        }

    }

    public void stopTaskCheckPay() {

        if (mTimer != null) {

            mTimer.cancel();
        }
        mTimer = null;
        mCheckPayTask = null;
    }

    /*
    * 验证取杯码是否有效
    * */
    public void checkQubeimaVilide(final String qubeima) {
        Runnable mRun = new Runnable() {

            @Override
            public void run() {
                Map<String, Object> params = ConstanceMethod.getParams();
                params.put("machineCode", MachineConfig.getMachineCode());
                params.put("cupCode", qubeima);
//                params.put("key",MachineConfig.get)

                MyLog.d(TAG, "checkQubeimaVilide RQ_URL = " + Constance.CHECK_QUBEIMA);
                MyLog.d(TAG, "checkQubeimaVilide params = " + JsonUtil.mapToJson(params));

                String RESPONSE_TEXT = null;
                try {
                    RESPONSE_TEXT = OkHttpUtil.post(Constance.CHECK_QUBEIMA, JsonUtil.mapToJson(params));
                } catch (IOException e) {
                    e.printStackTrace();
                }

                MyLog.W(TAG, "checkQubeimaVilide data" + RESPONSE_TEXT);

                if (RESPONSE_TEXT != null) {


                } else {

                }
            }
        };
        Thread t = new Thread(mRun);
        t.start();
    }
}
